home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- *
- * glwidget2.c - an example of a GL widget and a work procedure used
- * with the Motif toolkit.
- *
- * Ivan Hajadi
- * 12-June-1992
- *
- * to compile:
- * cc -o glwidget2 glwidget2.c -lXirisw -lgl_s -lXm -lXt -lX11_s -lPW -lsun
- */
-
- /* Motif includes */
- #include <Xm/Xm.h>
- #include <Xm/Form.h>
- #include <Xm/PushB.h>
-
- /* GL widget include */
- #include <X11/Xirisw/GlxMDraw.h>
-
-
- GLXconfig rgb_mode[] = {
- { GLX_NORMAL, GLX_RGB, TRUE },
- { GLX_NORMAL, GLX_DOUBLE, TRUE },
- { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
- { 0, 0, 0, }
- };
-
- XtAppContext app_context;
- XtWorkProcId work_procid = 0;
- int angle_x = 0, angle_y = 0;
-
- void drawscene(int, int);
-
- main(int argc, char** argv)
- {
- Widget top, form, quit_but, anim_but;
- Widget gl_widget;
-
- /* Prototypes */
- void installColormap(Widget toplevel, Widget glw);
- void quitCB(Widget, XtPointer, XtPointer);
- void ginitCB(Widget, XtPointer, XtPointer);
- void exposeCB(Widget, XtPointer, XtPointer);
- void animateCB(Widget, XtPointer, XtPointer);
-
- Arg args[20];
- int n;
-
- top = XtAppInitialize(&app_context, "Cube",
- NULL, 0, &argc, argv, NULL, NULL, 0
- );
- n=0;
- XtSetArg(args[n], XmNwidth, 730); n++;
- XtSetArg(args[n], XmNheight, 445); n++;
- form = XmCreateForm(top, "form", args, n);
- XtManageChild(form);
-
- /* Create GL widget */
-
- n = 0;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNleftOffset, 20); n++;
- XtSetArg(args[n], XmNrightOffset, 120); n++;
- XtSetArg(args[n], XmNbottomOffset, 40); n++;
- XtSetArg(args[n], XmNtopOffset, 40); n++;
- XtSetArg(args[n], XmNwidth, 500); n++;
- XtSetArg(args[n], XmNheight, 400); n++;
- XtSetArg(args[n], GlxNglxConfig, rgb_mode); n++;
- gl_widget = GlxCreateMDraw(form, "gl_widget", args, n);
- XtManageChild(gl_widget);
-
- /* Add callbacks */
-
- XtAddCallback(gl_widget, GlxNginitCallback, ginitCB, NULL);
- XtAddCallback(gl_widget, GlxNexposeCallback, exposeCB, NULL);
-
- n=0;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightOffset, 10); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomOffset, 10); n++;
- XtSetArg(args[n], XmNtraversalOn, False); n++;
- quit_but = XmCreatePushButton(form, " Quit ", args, n);
- XtManageChild(quit_but);
-
- n=0;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightOffset, 10); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomOffset, 50); n++;
- XtSetArg(args[n], XmNtraversalOn, False); n++;
- anim_but = XmCreatePushButton(form, "Rotate", args, n);
- XtManageChild(anim_but);
-
- XtAddCallback(quit_but, XmNactivateCallback, quitCB, NULL);
- XtAddCallback(anim_but, XmNactivateCallback, animateCB, NULL);
-
- XtRealizeWidget(top);
- installColormap(top, gl_widget);
- XtAppMainLoop(app_context);
- }
-
- /*
- * This routine will install a particular gl widgets's colormap onto the
- * top level window. It may not be called until after the windows have
- * been realized.
- */
- void installColormap(Widget toplevel, Widget glw)
- {
- Window windows[2];
-
- windows[0] = XtWindow(glw);
- windows[1] = XtWindow(toplevel);
- XSetWMColormapWindows(XtDisplay(toplevel), XtWindow(toplevel), windows, 2);
- }
-
- Boolean animate(XtPointer clientD)
- {
- angle_y += 75;
- angle_x += 75;
-
- if ((angle_y >= 3600) || (angle_y <= -3600))
- angle_y = 0;
- if ((angle_x >= 3600) || (angle_x <= -3600))
- angle_x = 0;
-
- drawscene(angle_x, angle_y);
-
- /*
- Return False so this WorkProc
- keeps getting called.
- */
- return False;
- }
-
- void animateCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- static Boolean started = False;
- Arg args[1];
- XmString xms;
-
- if (! started ) {
- Boolean animate(XtPointer);
- work_procid = XtAppAddWorkProc(app_context, animate, NULL);
- started = True;
- xms = XmStringCreateSimple(" Stop ");
- } else {
- XtRemoveWorkProc(work_procid);
- started = False;
- xms = XmStringCreateSimple("Rotate");
- }
-
- /* Changed the button label */
- XtSetArg(args[0], XmNlabelString, xms);
- XtSetValues(w, args, 1);
- XmStringFree(xms);
- }
-
- void ginitCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
-
- GLXwinset(XtDisplay(w), call_data->window);
-
- shademodel(GOURAUD);
- zbuffer(TRUE);
- subpixel(TRUE);
- lsetdepth(getgdesc(GD_ZMIN), getgdesc(GD_ZMAX));
- mmode(MVIEWING);
- perspective(450, 1, 1, 100);
- }
-
- void exposeCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
-
- GLXwinset(XtDisplay(w), call_data->window);
- drawscene(angle_x, angle_y);
- }
-
- void quitCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- exit(0);
- }
-
- void drawscene(int angle_x, int angle_y)
- {
- /* Draw smooth-shaded cube */
- static long v1[4][3] = {
- {-10, -10, 10},
- {10, -10, 10},
- {10, 10, 10},
- {-10, 10, 10},
- };
- static long v2[4][3] = {
- {-10, -10, -10},
- {-10, 10, -10},
- {10, 10, -10},
- {10, -10, -10},
- };
- static long colors1[] = {
- 0x000000FF,
- 0x00FF0000,
- 0x0000FF00,
- 0x00FFFF00,
- };
- static long colors2[] = {
- 0x0000FFFF,
- 0x00FF00FF,
- 0x00FFFFFF,
- 0x0FF00000,
- };
-
- register int i;
-
- reshapeviewport();
- czclear(0x00777777, getgdesc(GD_ZMAX));
- pushmatrix();
- polarview(80, 0, 250, 0);
-
- pushmatrix();
- rotate(angle_x, 'x');
- rotate(angle_y, 'y');
-
- bgnpolygon();
- for(i=0; i<4; i++) {
- cpack(colors1[i]);
- v3i(v1[i]);
- }
- endpolygon();
- bgnpolygon();
- for(i=0; i<4; i++) {
- cpack(colors2[i]);
- v3i(v2[i]);
- }
- endpolygon();
-
- bgnpolygon();
- cpack(colors1[1]);
- v3i(v1[1]);
- cpack(colors1[2]);
- v3i(v1[2]);
- cpack(colors2[2]);
- v3i(v2[2]);
- cpack(colors2[3]);
- v3i(v2[3]);
- endpolygon();
- bgnpolygon();
- cpack(colors1[0]);
- v3i(v1[0]);
- cpack(colors1[3]);
- v3i(v1[3]);
- cpack(colors2[1]);
- v3i(v2[1]);
- cpack(colors2[0]);
- v3i(v2[0]);
- endpolygon();
-
- bgnpolygon();
- cpack(colors1[2]);
- v3i(v1[2]);
- cpack(colors1[3]);
- v3i(v1[3]);
- cpack(colors2[1]);
- v3i(v2[1]);
- cpack(colors2[2]);
- v3i(v2[2]);
- endpolygon();
- bgnpolygon();
- cpack(colors1[1]);
- v3i(v1[1]);
- cpack(colors1[0]);
- v3i(v1[0]);
- cpack(colors2[0]);
- v3i(v2[0]);
- cpack(colors2[3]);
- v3i(v2[3]);
- endpolygon();
-
- popmatrix();
- popmatrix();
-
- swapbuffers();
- }
-
-